from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
from typing import List, Optional
from app.services.article_service import ArticleService

router = APIRouter()
article_service = ArticleService()

class ArticleCreate(BaseModel):
    topic: str
    references: List[str]
    length: str
    style: str

class Article(ArticleCreate):
    id: int
    content: str
    created_at: str
    updated_at: str

@router.post("/articles/generate")
async def generate_article(article_data: ArticleCreate):
    try:
        return await article_service.generate_article(
            topic=article_data.topic,
            references=article_data.references,
            length=article_data.length,
            style=article_data.style
        )
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@router.get("/articles")
async def get_articles():
    return await article_service.get_all_articles()

@router.get("/articles/{article_id}")
async def get_article(article_id: int):
    article = await article_service.get_article(article_id)
    if not article:
        raise HTTPException(status_code=404, detail="Article not found")
    return article 